<?php require_once(dirname(__FILE__)."/../config/config.php"); ?>
<?php
/**
 * API返回结果
 * 0499,开启全路由成功
 * 0498,关闭全路由成功
 * 0497,开启智能路由成功
 * 0496,关闭智能路由成功
 * 0489:routes状态更新成功
 * 0488:DNS状态更新成功
 * 0487:WIFI状态更新成功
 * 0416:设备名不存在
 * 
 */
if ($_REQUEST['method'] == "ssid_add") {
	$message=addssid();
	echo json_encode($message);
}
if ($_REQUEST['method'] == "ssid_update") {
	$message=updatessid();
	echo json_encode($message);
}
if ($_REQUEST['method'] == "dns_update") {
	$message=updatedns();
	echo json_encode($message);
}
if ($_REQUEST['method'] === "ssid_delete") {
	$message=deletessid();
	echo json_encode($message);
}
if ($_REQUEST['method'] === "deleteAllssid") {
	$message=deleteAllssid();
	echo json_encode($message);
}if ($_REQUEST['method'] === "setFullRouting") {
	$message=setFullRouting();
	echo json_encode($message);
}if ($_REQUEST['method'] === "setRouting") {
	$message=setRouting();
	echo json_encode($message);
}if ($_REQUEST['method'] === "getRoutingStatus") {
	$message=getRoutingStatus();
	echo json_encode($message);
}if($_REQUEST['method'] === "getUpdateStatus"){
	$content = getUpdateStatus();
	outPutXml($content);
}
if($_REQUEST['method'] === "getequipment")
{
    $message=getequipment();
    echo json_encode($message);
}
if($_REQUEST['method'] === "UpdateStatus"){
	if(isset($_GET['mac'])){
		$type = $_GET['update'];
		$sql="select * from zy_equipment where MacAddress='".$_GET['mac']."'";
		$rs = mysql_query($sql);
		$rows = mysql_fetch_array($rs);
	}
	$flag=0;
	$message = updateStatus($rows['updater'],$type,$flag);
	outPutXml($message);
}

//新增SSID
function addssid() {
	//$user_id=$_REQUEST['user_id'];
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	
	$sqluser = "select * from zy_user where user_id='" . $user_id . "'";
	$rs1 = mysql_query($sqluser);
	$row1 = mysql_fetch_array($rs1);
	$name = $_REQUEST['name'];
	$password = $_REQUEST['spassword'];
	$priority = $_REQUEST['priority'];
	$speedlimit = $_REQUEST['speedlimit'];
	$sql = "insert into zy_routersettings(name,password,priority,speedlimit,user_id,is_delete,crate_time,crate_user) values('" . $name . "','" . $password . "'," . $priority . "," . $speedlimit . "," . $user_id . ",0,'" . date('Y-m-d H:i:s') . "','" . $row1['user_name'] . "')";
	
	if (mysql_query($sql)) {
		$inserted = mysql_query("SELECT @@IDENTITY");
		$inserted_rs= mysql_fetch_array($inserted );
		$inserted_id=$inserted_rs['0'];
		$message = array (
			'message' => 'insertSSIDsuccess',
			'insert_id'=>$inserted_id
		);
	} else {
		$message = array (
			'message' => 'insertSSIDfailer'
		);
	}
	return $message;
}

//修改SSID
function updatessid() {
	$id = $_REQUEST['id'];
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	$sqluser = "select * from zy_user where user_id='" . $user_id . "'";
	$rs1 = mysql_query($sqluser);
	$row1 = mysql_fetch_array($rs1);
	$name = $_REQUEST['name'];
	$password = $_REQUEST['spassword'];
	$priority = $_REQUEST['priority'];
	$speedlimit = $_REQUEST['speedlimit'];
	$sql = "update zy_routersettings SET name='" . $name . "',password='" . $password . "',priority=" . $priority . ",speedlimit=" . $speedlimit . ",update_time='" . date("Y-m-d H:i:m") . "',update_user='" . $row1['user_name'] . "'where user_id=" . $user_id . " and id=" . $id;
	if (mysql_query($sql)) {
		$message = array (
			'message' => 'updateSSIDsuccess'
		);
	} else {
		$message = array (
			'message' => 'updateSSIDfailer'
		);
	}
	return $message;
}

//删除SSID
function deletessid() {
	$id = $_REQUEST['id'];
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	$sqldeletessid = "delete from zy_routersettings where id=" . $id . " and user_id=" . $user_id . "";
	if (mysql_query($sqldeletessid)) {
		$message = array (
			'message' => 'deleteSSIDsuccess'
		);
	} else {
		$message = array (
			'message' => 'deleteSSIDfailer'
		);
	}
	return $message;
}
//删除所有SSID
function deleteAllssid()
{
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	$sqldeleteAllssid="delete from zy_routersettings where user_id=".$user_id."";
	if(mysql_query($sqldeleteAllssid))
	{
		$message=array('message'=>'deleteAllSSIDsuccess');
	}else{
		$message=array('message'=>'deleteAllSSIDfailer');
	}
	return $message;
}
//修改4个DNS
function updatedns() {
	$dns = $_REQUEST['dns'];
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	$sql = "select * from zy_dnslist where user_id='" . $user_id . "'";
	$rs = mysql_query($sql);
	if($row = mysql_fetch_array($rs)){
		$sqldns = "update zy_dnslist set dns1='" . $dns['0'] . "',dns2='" . $dns['1'] . "',dns3='" . $dns['2'] . "',dns4='" . $dns['3'] . "',update_time='" . date("Y-m-d H:i:m") . "',update_user='" . $user_id. "' where user_id='" .$user_id. "'";
	}else{
		$now_time=date('Y-m-d H:i:m');
		$sqldns="insert into zy_dnslist (user_id,dns1,dns2,dns3,dns4,create_user,create_time) values($user_id,'$dns[0]','$dns[1]','$dns[2]','$dns[3]',$user_id,'$now_time')";
	}
	if (mysql_query($sqldns)) {
		$message = array (
			'message' => 'updateDNSsuccess'
		);
	} else {
		$message = array (
			'message' => 'updateDNSfailer'
		);
	}
	return $message;
}
//开启全路由
function setFullRouting() {
	$status = $_REQUEST['status'];
	$vpnlist_id = $_REQUEST['vpnlist_id'];
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	$sql = "select * from zy_full_routing where user_id='" . $user_id . "'";
	$rs = mysql_query($sql);
	if($row = mysql_fetch_array($rs)){
		$now_time=date('Y-m-d H:i:m');
		$sqldns = "update zy_full_routing set vpnlist_id=" . $vpnlist_id . ",mark=" .$status .", open_time='".$now_time."', create_time='".$now_time."' where user_id='" .$user_id. "'";
	}else{
		$now_time=date('Y-m-d H:i:m');
		$sqldns="insert into zy_full_routing (vpnlist_id,mark,open_time,user_id,create_time) values($vpnlist_id,$status,'$now_time',$user_id,'$now_time')";
	}
	if (mysql_query($sqldns)) {
		$message = array (
			'message' => 'setFullRoutingsuccess'
		);
	} else {
		$message = array (
			'message' => 'setFullRoutingfailer'
		);
	}
	return $message;
}
function showuseridBymacaddress($mac_address) {
	$sqlmac = "select * from zy_equipment  where MacAddress='" . $mac_address . "'";
	$rs = mysql_query($sqlmac);
	$row = mysql_fetch_array($rs);
	return $row['updater'];
}
function showuseridByUsernameAndPassword($username, $password) {
	$sql = "select * from zy_user where user_name = '" . $username . "' and password = '" . $password . "'";
	$result = mysql_query($sql);
	$rs = mysql_fetch_array($result);
	return $rs['user_id'];
}


//smart routing部分的开通及关闭，用户许可协议
function setRouting()
{
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
		$mark=$_REQUEST['mark'];
		$smart_mark=$_REQUEST['smart_mark'];
		$vpnlist_id=$_REQUEST['vpnlist_id'];//用户开通的那个路由id
		$SN=$_REQUEST['SN'];
		
		$time=date("Y-m-d H:i:s");
		if(getenv('HTTP_CLIENT_IP')) { 
			$onlineip = getenv('HTTP_CLIENT_IP');
		} elseif(getenv('HTTP_X_FORWARDED_FOR')) { 
			$onlineip = getenv('HTTP_X_FORWARDED_FOR');
		} elseif(getenv('REMOTE_ADDR')) { 
			$onlineip = getenv('REMOTE_ADDR');
		} else { 
			$onlineip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
		}
    	 if($mark==1)
			{
				$sql="insert into zy_record(user_id,IP,SN,operate_content,create_time) values($user_id,'$onlineip',$SN,'Open Full Routing','$time')";
				mysql_query($sql);
			}else if ($smart_mark==1)
			{
				$sql="insert into zy_record(user_id,IP,SN,operate_content,create_time) values($user_id,'$onlineip',$SN,'Open Smart Routing','$time')";
				mysql_query($sql);
			}			  
		//改路由状态为1
		$flag=1;
		//无论开启职能路由还是全路由 均更新routeupdate 状态为1
		$type="routes";
		$message = updateStatus($user_id,$type,$flag);
		//判断是否存在该用户的信息  如果不存在就添加信息   存在的话就修改信息
		$sql="select * from zy_full_routing where user_id=".$user_id;
		$rs=mysql_query($sql);
		if($row=mysql_fetch_array($rs))//存在用户的信息
		{
				if(isset($_REQUEST['mark']))//开通的是全路由  先判断是否开通 mark=2
				{
					if($mark==0)//关闭路由
					{
						$sql="update zy_full_routing set mark=0,open_time='".date('0000-00-00 00:00:00')."',vpnlist_id=0 where user_id=$user_id";
						mysql_query($sql);
						return array('message'=>'0498');
					}else if($mark==1){//开启路由
						$sql="update zy_full_routing set mark=1,open_time='$time',vpnlist_id=$vpnlist_id where user_id=$user_id";
						mysql_query($sql);
						return array('message'=>'0499');
					}			
			  }elseif (isset($_REQUEST['smart_mark'])){//开通的是智能路由   smart_mark=1		
					if($smart_mark==0)//关闭路由
					{
						$sql="update zy_full_routing set smart_mark=0 where user_id=$user_id";
						mysql_query($sql);
						return array('message'=>'0496');
					}else if($smart_mark==1){//开启路由
						$sql="update zy_full_routing set smart_mark=1 where user_id=$user_id";
						mysql_query($sql);
						return array('message'=>'0497');
					}
				
				}	
		 }else{//不存在用户的信息,表示第一次开启全路由或者只能路由.
				if(isset($_REQUEST['mark']))//开通的是全路由  先判断是否开通  mark=1
				{
					$mark==0?$time=date('0000-00-00 00:00:00'):$time;				
					$sql="insert into zy_full_routing(vpnlist_id,open_time,mark,user_id,create_time) values($vpnlist_id,'".date('Y-m-d H:i:s')."',$mark,$user_id,'".date('Y-m-d H:i:s')."')";
					$rs=mysql_query($sql);
					return array('message'=>'0499');
				}elseif (isset($_REQUEST['smart_mark']))//开通的是智能路由 mark=1
				{
					$sql="insert into zy_full_routing(vpnlist_id,smart_mark,user_id,create_time) values(0,$smart_mark,$user_id,'".date('Y-m-d H:i:s')."')";
					$rs=mysql_query($sql);
					return array('message'=>'0497');
				}	
			}

}
function getRoutingStatus(){
	if(isset($_REQUEST['macAddress'])){
		$macAddress = $_REQUEST['macAddress'];
		$user_id = showuseridBymacaddress($macAddress);
	}else if(isset($_REQUEST['username'])){
		$user_id = showuseridByUsernameAndPassword($_REQUEST['username'],$_REQUEST['password']);
	}
	if(!$user_id>0){
		$message = array (
			'message' => 'login fail'
		);
		return $message;
	}
	//判断是否存在该用户的信息  如果不存在就添加信息   存在的话就修改信息
		$sql="select * from zy_full_routing where user_id=".$user_id;
		$rs=mysql_query($sql);
		if($row=mysql_fetch_array($rs))//存在用户的信息
		{
			return array('mark'=>$row['mark'],'smart_mark'=>$row['smart_mark'],'vpnlist_id'=>$row['vpnlist_id']);
		}else{
			return array('mark'=>0,'smart_mark'=>0);
		}
}


/**
 * 当API取出这条记录时,把routeupdate 更新为 0:未有更新记录
 * 0489:routes状态更新成功
 * 0488:DNS状态更新成功
 * 0487:WIFI状态更新成功
 */
function updateStatus($user_id,$type,$flag){
	$content = "";
	if($type=="routes"){
		$sql="UPDATE zy_updatestatus set routeupdate=".$flag." where user_id ='".$user_id."'";
		$content= "<result>0489</result>\n";
	}
	if($type=="dns"){
		$sql="UPDATE zy_updatestatus set dnsupdate=".$flag." where user_id ='".$user_id."'";
		$content= "<result>0488</result>\n";
	}
	if($type=="wifi"){
		$sql="UPDATE zy_updatestatus set wifiupdate=".$flag." where user_id ='".$user_id."'";
		$content= "<result>0487</result>\n";
	}
	$rs = mysql_query($sql);
	if($rs > 0){
		return $content;
	}

}
/**
 * 获取路由器更新状态
 * @return string
 */
function getUpdateStatus(){
	$content = "";
	if(isset($_GET['mac'])){
		$sql1="select * from zy_equipment where MacAddress='".$_GET['mac']."'";
		$rss = mysql_query($sql1);
		$rows = mysql_fetch_array($rss);

		$sql="select * from zy_updatestatus where user_id='".$rows['updater']."'";
		$rs = mysql_query($sql);
		$row = mysql_fetch_array($rs);

		$content.= "<result>\n";
		$content.="<routes>".$row['routeupdate']."</routes>\n";
		$content.="<dns>".$row['dnsupdate']."</dns>\n";
		$content.="<wifi>".$row['wifiupdate']."</wifi>\n";
		$content.= "</result>\n";
	}
	return $content;
}


function getequipment()
{
	
    $user_id=$_REQUEST['user_id'];
    $mac=$_REQUEST['macAddress'];
    if($user_id!=null)
    { 
    	$sql="select * from zy_equipment where service_id=8 and updater=".$user_id;	    
    }
 	else if($mac!=null)
    {
    	$sql="select * from zy_equipment where MacAddress='".$mac."'";
    }
     $rs=mysql_query($sql);
    if($row=mysql_fetch_row($rs)){
    	$result=getequipmentName($row[1]); 	
    	$message=$result;
    }else{
    	$message=array("message"=>"0416");
    }
    return  $message;     
}

function getequipmentName($equipment_name)
{
    $arr = array(
            "multi WIFI",
            "WIFI username & password",
            "WIFI speed limit",
            "Smart routing",
            "Full routing",
            "DNS"
    );
    $result=array();
    if ($equipment_name == 3020) {
        $result = array(
                $arr[0] => "1",
                $arr[1] => "1",
                $arr[2] => "1",
                $arr[3] => "1",
                $arr[4] => "1",
                $arr[5] => "1"
        )
        ;
    }
    if ($equipment_name == 3631) {
        $result= array(
                $arr[0] => "0",
                $arr[1] => "0",
                $arr[2] => "0",
                $arr[3] => "1",
                $arr[4] => "1",
                $arr[5] => "0"
        )
        ;
    }
    if ($equipment_name == 842) {
        $result= array(
                $arr[0] => "1",
                $arr[1] => "1",
                $arr[2] => "1",
                $arr[3] => "1",
                $arr[4] => "1",
                $arr[5] => "1"
        )
        ;
    }
    if ($equipment_name == 1200) {
        $result = array(
                $arr[0] => "0",
                $arr[1] => "0",
                $arr[2] => "0",
                $arr[3] => "1",
                $arr[4] => "1",
                $arr[5] => "0"
        )
        ;
    
    }
    
    return $result;
    
    
}
/**
 * 输出XML格式内容
 * @param unknown $content
 */
function outPutXml($content) {
	header("Content-type:text/xml");
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n";
	echo "<netcuberesult>\n";
	echo $content;
	echo "</netcuberesult>";
	die();
}

?>